<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="车牌号">
              <a-input placeholder="请输入车牌号" v-model="queryParam.plate"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="车架号">
              <a-input placeholder="请输入车架号" v-model="queryParam.vin"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="审核状态">
              <!-- <a-input placeholder="审核状态" v-model="queryParam.auditstatus"></a-input> -->
              <j-dict-select-tag placeholder="请输入单据状态" v-model="queryParam.auditstatus" dictCode="auditflag"
                :async="true">
              </j-dict-select-tag>

            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="事故号">
                <a-input placeholder="请输入事故号" v-model="queryParam.accidentNumber"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button @click="handleSubmit" type="primary" icon="plus" v-show="firstshow">提交审核</a-button>
      <a-button @click="handleApproved" type="primary" icon="plus" v-show="regionshow1">地区审批通过</a-button>
      <a-button @click="handleNotApproved" type="primary" icon="plus" v-show="regionshow2">地区审批不通过</a-button>
      <a-button @click="handleProvincApproved" type="primary" icon="plus" v-show="provincshow1">省公司审批通过</a-button>
      <a-button @click="handleProvinceNotApproved" type="primary" icon="plus" v-show="provincshow2">省公司审批不通过</a-button>
      <a-button @click="handlevendor" type="primary" icon="plus" v-show="vendorshow">指派供应商</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('询价单')">导出</a-button>
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
        @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" bordered rowKey="id" class="j-table-force-nowrap" :scroll="{ x: true }"
        :columns="columns" :dataSource="dataSource" :pagination="ipagination" :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="pcaSlot" slot-scope="text">
          <div>{{ getPcaText(text) }}</div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)"
            v-show="record.auditstatus == '6' || record.auditstatus == '2' || record.auditstatus == '4'">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a v-show="record.auditstatus == '6' || record.auditstatus == '2' || record.auditstatus == '4'">删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <scmxjd-modal ref="modalForm" @ok="modalFormOk" />
    <a-modal v-model="visible" title="请录入驳回意见" ok-text="确认" cancel-text="取消" @ok="noapproved" @cancel="canclenoapproved">
      <a-textarea type="textarea" v-model="rejectreason" :rows="4" />
    </a-modal>
    <a-modal v-model="provvisible" title="请录入驳回意见" ok-text="确认" cancel-text="取消" @ok="provincenoapproved"
      @cancel="canclenoapproved">
      <a-textarea type="textarea" v-model="rejectreason" :rows="4" />
    </a-modal>
    <a-modal v-model="vendorvisible" title="请选择供应商" ok-text="确认" cancel-text="取消" @ok="selectvendor"
      @cancel="canclenoapproved">
      <a-form>
        <a-form-item>
          <!-- <j-multi-select-tag v-model="vendorlist" :options="vendorOptions"></j-multi-select-tag> -->
          <!-- <j-popup v-model="vendorlistname" code="vendorhelp" field="partnername" orgFields="partnername,partnercode,id"
            destFields="partnername,partnercode,id" :multi="true" @input="popupCallback"></j-popup> -->
          <c-select-vendor v-model="vendorlist" :mult="true" :buttons="false" placeholder="请输入供应商"></c-select-vendor>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ScmxjdModal from './modules/ScmxjdModal'
import Area from '@/components/_util/Area'
import '@/assets/less/TableExpand.less'
import { getAction, postAction } from '../../../../api/manage'
import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
import { getpartnerlist, getpartnernamebyids } from '@/api/erp/commonapi'

export default {
  name: 'ScmxjdList',
  mixins: [JeecgListMixin],
  components: {
    ScmxjdModal,
    JMultiSelectTag
  },
  data () {
    return {
      description: '询价单管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: "询价单号",
          align: "center",
          dataIndex: "billcode",
        },
        {
          title: '地区',
          align: 'center',
          dataIndex: 'cityid',
          scopedSlots: { customRender: 'pcaSlot' }
        },
        // {
        //   title:'地区',
        //   align:"center",
        //   dataIndex: 'regionid',
        //   scopedSlots: {customRender: 'pcaSlot'}
        // },
        {
          title: '车辆信息',
          align: 'center',
          dataIndex: 'vehicle'
        },
        {
          title: '车牌号',
          align: 'center',
          dataIndex: 'plate'
        },
        {
          title: '车架号',
          align: 'center',
          dataIndex: 'vin'
        },
        // {
        //   title: '行车证图片',
        //   align: "center",
        //   dataIndex: 'licenseimageString'
        // },
        {
          title: '事故号',
          align: 'center',
          dataIndex: 'accidentNumber'
        },
        {
          title: "行驶证",
          align: "center",
          dataIndex: "licenseimageString",
          scopedSlots: {
            customRender: 'imgSlot'
          }
        },
        {
          title: '事故图片',
          align: "center",
          dataIndex: "accidentimagesString",
          scopedSlots: {
            customRender: 'imgSlot'
          }
        },
        {
          title: '单据状态',
          align: 'center',
          dataIndex: 'status',
          customRender: (t, r, i) => {
            if (t == '0') {
              return '删除'
            } else if (t == '1') {
              return '提交待审核'
            } else if (t == '2') {
              return '地区审核通过'
            } else if (t == '3') {
              return '地区审核不通过'
            } else if (t == '4') {
              return '省公司审核通过待指定'
            } else if (t == '5') {
              return '省公司审核不通过'
            } else if (t == '6') {
              return "已分配供应商"
            } else if (t == '7') {
              return '已报价'
            } else if (t == '8') {
              return "已下订单"
            }
          }
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'auditstatus',
          customRender: (t, r, i) => {
            if (t == '0') {
              return '待审核'
            } else if (t == '1') {
              return '地区审核'
            } else if (t == '2') {
              return '地区审核未通过'
            } else if (t == '3') {
              return '省公司审核'
            } else if (t == '4') {
              return '省公司审核未通过'
            } else if (t == '5') {
              return '审核通过'
            } else if (t == '6') {
              return '待提交审核'
            }
          }
        },
        {
          title: '配件数量',
          align: 'center',
          dataIndex: 'partcount'
        },
        {
          title: '定损员',
          align: 'center',
          dataIndex: 'createbyname'
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime'
        },
        // {
        //   title: "供应商",
        //   align: "center",
        //   dataIndex: "reqvendorname",
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      visible: false,
      rejectreason: '',
      provincshow1: false,
      provincshow2: false,
      regionshow1: false,
      regionshow2: false,
      firstshow: false,
      vendorshow: false,
      provvisible: false,
      vendorvisible: false,
      vendorlist: '',
      vendorlistname: '',
      vendorOptions: [],
      provinceflag: '',
      partnercode: '',
      partnername: '',
      selectedRows: [],
      url: {
        list: '/rfq.xbj/scmxjd/list',
        delete: '/rfq.xbj/scmxjd/delete',
        deleteBatch: '/rfq.xbj/scmxjd/deleteBatch',
        exportXlsUrl: '/rfq.xbj/scmxjd/exportXls',
        importExcelUrl: 'rfq.xbj/scmxjd/importExcel',
        updateStatusUrl: '/rfq.xbj/scmxjd/submitxjd',
        updatevendorUrl: 'rfq.xbj/scmxjd/updatevendor',
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  watch: {
    "queryParam.auditstatus": {
      handler (val, oldval) {
        if (val == '3' && this.provinceflag === '0') {
          this.vendorshow = true;
        } else {
          this.vendorshow = false;
        }
      }
    }
  },
  async created () {
    this.pcaData = new Area()
    this.getSuperFieldList()
    let currentuser = this.$store.getters.userInfo
    console.log(currentuser)
    //provinceflag
    this.provinceflag = currentuser.provinceflag
    if (this.provinceflag) {
      if (this.provinceflag === '0') {
        // this.provincshow = true
        // this.vendorshow = true
        let partnerlist = await getpartnerlist({ partnertype: '0' });
        if (partnerlist) {
          let presu = partnerlist.result;
          for (let i = 0; i < presu.length; i++) {
            this.vendorOptions.push({
              text: presu[i].partnername,
              value: presu[i].id
            })
          }
        }
      } else if (this.provinceflag === '1') {
        // this.regionshow = true
      } else if (this.provinceflag === '2') {
        // this.firstshow = true
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      let auditstatus = selectedRows[0]['auditstatus']
      if (auditstatus) {
        if (auditstatus === '0') {
          if (this.provinceflag === '1') {
            this.regionshow1 = true;
            this.regionshow2 = true;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '1') {
          if (this.provinceflag === '0') {
            this.provincshow1 = true;
            this.provincshow2 = true;
          } else if (this.provinceflag === '1') {
            this.regionshow1 = false;
            this.regionshow2 = false;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '2') {
          if (this.provinceflag === '1') {
            this.regionshow1 = false;
            this.regionshow2 = false;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '3') {
          if (this.provinceflag === '0') {
            this.provincshow1 = false;
            this.provincshow2 = false;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '4') {
          if (this.provinceflag === '0') {
            this.provincshow1 = false;
            this.provincshow2 = false;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '5') {
          if (this.provinceflag === '0') {
            this.provincshow1 = false;
            this.provincshow2 = false;
          } else if (this.provinceflag === '1') {
            this.regionshow1 = false;
            this.regionshow2 = false;
          } else if (this.provinceflag === '2') {
            this.firstshow = false;
          }
        } else if (auditstatus === '6') {
          if (this.provinceflag === '2') {
            this.firstshow = true;
          }
        }
      }
    },
    // popupCallback (str, res) {
    //   // console.log(res);
    //   this.vendorlist = res.id;
    // },
    selectvendor () {
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          vendorid: this.vendorlist
        })
      }
      postAction(this.url.updatevendorUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('指派供应商成功')
          this.vendorvisible = false;
          this.loadData(1);
          this.vendorlist = '';
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handlevendor () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '2'
        })
      }
      this.vendorvisible = true
    },
    provincenoapproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '4',
          rejectreason: this.rejectreason
        })
      }
      if (this.rejectreason === '') {
        this.$message.error('请录入驳回意见')
        return
      }
      this.provvisible = false
      // console.log("ssss"+this.rejectreason);
      postAction(this.url.updateStatusUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('审批不通过')
          return
        } else {
          this.$message.error(res.message);
        }
      })
    },
    handleProvinceNotApproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '2'
        })
      }
      this.provvisible = true
    },
    handleProvincApproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '3'
        })
      }

      postAction(this.url.updateStatusUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('审核通过')
          this.loadData(1)
          return
        } else {
          this.$message.error(res.message);
        }
      })
    },
    canclenoapproved () {
      this.visible = false
      this.provvisible = false
      this.vendorvisible = false
    },
    noapproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '2',
          rejectreason: this.rejectreason
        })
      }
      if (this.rejectreason === '') {
        this.$message.error('请录入驳回意见')
        return
      }
      this.visible = false
      // console.log("ssss"+this.rejectreason);
      postAction(this.url.updateStatusUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('审批不通过')
          return
        } else {
          this.$message.error(res.message);
        }
      })
    },
    handleNotApproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '2'
        })
      }
      this.visible = true
    },
    handleApproved () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '1'
        })
      }

      postAction(this.url.updateStatusUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('审核通过')
          this.loadData(1)
          return
        }
      })
    },
    handleSubmit () {
      if (this.selectedRowKeys.length === 0) {
        this.$message.error('请先选择一张询价单提交')
        return
      }
      if (this.selectedRows[0]["status"] === '0') {
        this.$message.error('当前询价单已被删除，不允许提交！');
        return;
      }
      let ids = []
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        ids.push({
          id: this.selectedRowKeys[i],
          auditstatus: '0'
        })
      }
      postAction(this.url.updateStatusUrl, { id: ids }).then(res => {
        if (res.success) {
          this.$message.success('提交成功')
          this.loadData(1)
          return
        } else {
          this.$message.error(res.message)
          return
        }
      })
    },
    getPcaText (code) {
      return this.pcaData.getText(code)
    },
    initDictConfig () { },
    getSuperFieldList () {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'createBy', text: '创建人', dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'createTime', text: '创建日期' })
      fieldList.push({ type: 'pca', value: 'cityid', text: '城市' })
      fieldList.push({ type: 'pca', value: 'regionid', text: '地区' })
      fieldList.push({ type: 'string', value: 'vehicle', text: '车辆信息', dictCode: '' })
      fieldList.push({ type: 'string', value: 'plate', text: '车牌号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'vin', text: '车架号', dictCode: '' })
      fieldList.push({ type: 'Blob', value: 'licenseimage', text: '行车证图片', dictCode: '' })
      fieldList.push({ type: 'string', value: 'accidentNumber', text: '事故号', dictCode: '' })
      fieldList.push({ type: 'Blob', value: 'accidentimages', text: '事故图片', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '单据状态', dictCode: '' })
      fieldList.push({ type: 'string', value: 'auditstatus', text: '审核状态', dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'partcount', text: '配件数量', dictCode: '' })
      fieldList.push({ type: 'string', value: 'bah', text: '报案号', dictCode: '' })
      fieldList.push({ type: 'string', value: 'xlcaddress', text: '修理厂地址', dictCode: '' })
      fieldList.push({ type: 'string', value: 'xlclink', text: '修理厂联系方式', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
